package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.Place;
import com.wzq.service.IOtherService;
import com.wzq.util.StringUtil;
import com.wzq.dao.pojo.Other;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/other")
public class AdminOtherController extends  GetSome{

    @Autowired
    IOtherService iOtherService;
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Other> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Other> pageResult = iOtherService.page(page, new QueryWrapper<Other>().in(StringUtil.isNotEmpty(query), "p_id", searchByPname(query)));
        //        添加用户名称和项目名称
        List<Other> list=new ArrayList<>();
        for (Other other : pageResult.getRecords()) {
            other.setP_name( getP_name(other.getP_id()) );
            list.add(other);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("OtherList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

//
//    @RequestMapping("/list")
//    public R list(@RequestBody PageBean pageBean){
//        System.out.println(pageBean);
//        String query=pageBean.getQuery().trim();
//        Page<Other> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
//        Page<Other> pageResult = iOtherService.page(page, new QueryWrapper<Other>().like(StringUtil.isNotEmpty(query), "p_id", query));
//        Map<String,Object> map=new HashMap<>();
//        map.put("OtherList",pageResult.getRecords());
//        map.put("total",pageResult.getTotal());
//        return R.ok(map);
//    }




    @RequestMapping("/save")
    public R save(@RequestBody Other other){
        if(other.getId()==null || other.getId()==-1){
            iOtherService.save(other);
        }else{
            iOtherService.saveOrUpdate(other);
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Other other = iOtherService.getById(id);
        //        获得项目名称
        other.setP_name( getP_name(other.getP_id()) );
        Map<String,Object> map=new HashMap<>();
        map.put("other",other);

        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iOtherService.removeById(id);
        return R.ok();
    }
}

